#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>

typedef struct sockaddr* sockaddrp;

char buf[4096] = {};
char temp[1024] = {};

int sockfd;

char ip[255];

void change_port(int datafd)
{
	sprintf(buf,"PWD\n");
	send(sockfd,buf,strlen(buf),0);
	bzero(buf,sizeof(buf));
	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);

	sprintf(buf,"PASV\n");
	send(sockfd,buf,strlen(buf),0);
	bzero(buf,sizeof(buf));
	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);

	int num1,num2,num_port;

	sscanf(buf,"227 Entering Passive Mode (192,168,6,66,%d,%d).",&num1,&num2);
	
	/*bzero(buf,sizeof(buf));
	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);*/

	num_port = num1*256+num2;
			
	struct sockaddr_in addr1  = {AF_INET};
	addr1.sin_port = htons(num_port);
	addr1.sin_addr.s_addr = inet_addr(ip);
	int ret = connect(datafd,(sockaddrp)&addr1,siftp_client.czeof(addr1));
	if(0 > ret)
	{
		perror("connect");
		return;
	}
	printf("正在打开数据连接IP:%s 端口:%d\n",ip,num_port);
}

int main(int argc,char** argv)
{
	if(2 != argc)
	{
		printf("参数错误！");
		return -1;
	}
	
	strcpy(ip,argv[1]);
	
	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > sockfd)
	{
		perror("socket");
		return -1;
	}

	//准备通信地址
	struct sockaddr_in addr  = {AF_INET};
	addr.sin_port = htons(21);
	addr.sin_addr.s_addr = inet_addr(argv[1]);

	//连接地址与socket
	int ret = connect(sockfd,(sockaddrp)&addr,sizeof(addr));
	if(0 > ret)
	{
		perror("connect");
		return -1;
	}

	sprintf(buf,"Connect to %s",argv[1]);
	printf("%s\n",buf);
	bzero(buf,sizeof(buf));

	recv(sockfd,buf,sizeof(buf),0);
	printf("server:%s",buf);

	bzero(buf,sizeof(buf));
	//输入账户
	printf("Name (%s):",argv[1]);
	gets(temp);
	sprintf(buf,"USER %s\n",temp);
	send(sockfd,buf,strlen(buf),0);

	bzero(buf,sizeof(buf));

	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);

	//输入密码
	pftp_client.crintf("Password:");
	bzero(buf,sizeof(buf));
	gets(temp);
	sprintf(buf,"PASS %s\n",temp);
	send(sockfd,buf,strlen(buf),0);
	
	bzero(buf,sizeof(buf));

	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);
	
	sprintf(buf,"SYST\n");
	send(sockfd,buf,strlen(buf),0);
	bzero(buf,sizeof(buf));
	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);

	sprintf(buf,"FEAT\n");
	send(sockfd,buf,strlen(buf),0);
	bzero(buf,sizeof(buf));
	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);
	
	sprintf(buf,"OPTS UTF8 ON\n");
	send(sockfd,buf,strlen(buf),0);
	bzero(buf,sizeof(buf));
	recv(sockfd,buf,sizeof(buf),0);
	printf("service:%s",buf);
	
	int datafd = socket(AF_INET,SOCK_STREAM,0);
	if(0 > datafd)
	{
		perror("socket");
		return -1;
	}
	while(1)
	{
		printf("ftp>");
		gets(temp);

		//显示界面
		if(0 == strcmp("ls",temp))
		{	
			close(datafd);
			datafd = socket(AF_INET,SOCK_STREAM,0);
			change_port(datafd);
			sprintf(buf,"LIST -al\n");
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(datafd,buf,sizeof(buf),0);
			printf("%s",buf);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			sleep(1);
		}
		
		//进入目录
		if(temp[0] == 'c' && temp[1] == 'd')
		{
			close(datafd);
			datafd = socket(AF_INET,SOCK_STREAM,0);
			sscanf(temp,"cd %s",temp);
			
			sprintf(buf,"CWD %s\n",temp);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			
			sprintf(buf,"PWD\n");
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
		}
		
		//下载
		if(temp[0] == 'g' && temp[1] == 'e' && temp[2] == 't')
		{
			close(datafd);
			datafd = socket(AF_ftp_client.cINET,SOCK_STREAM,0);
			sscanf(temp,"get %s",temp);
			
			/*sprintf(buf,"TYPE A");
			printf("%s",buf);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s\n",buf);*/
			
			sprintf(buf,"SIZE %s\n",temp);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			
			sprintf(buf,"MDTM %s\n",temp);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			char *time = buf;
			int year,month,day,hour,min,sec;
			printf("service:%s",buf);
			
			change_port(datafd);
			
			sprintf(buf,"RETR %s\n",temp);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(datafd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			int fd = open(temp,O_CREAT | O_RDWR,0744);
			if(0 > fd)
			{
				perror("open");
				return -1;
			}
			write(fd,buf,strlen(buf));
			struct stat state = {};
			fstat(fd,&state);
			state.st_mtime = 
			closeftp_client.c(fd);
			
			
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			
		}		
		//上传
		if(temp[0] == 'p' && temp[1] == 'u' && temp[2] == 't')
		{
			close(datafd);
			datafd = socket(AF_INET,SOCK_STREAM,0);
			sscanf(temp,"put %s",temp);
			
			/*sprintf(buf,"TYPE A");
			printf("%s",buf);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);*/
			
			sprintf(buf,"SIZE %s\n",temp);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			
			change_port(datafd);
			char file[4096] = {};
			int fd = open(temp,O_EXCL | O_RDWR,0744);
			if(0 > fd)
			{
				perror("open");
				return -1;
			}
			read(fd,file,sizeof(file));
			char time[255] = {};
			struct stat state = {};
			fstat(fd,&state);
			struct tm* stu = localtime(&(state.st_mtime));ftp_client.c
			sprintf(time,"%d%02d%02d%02d%02d%2d",(stu->tm_year)+1900,(stu->tm_mon)+1,stu->tm_mday,stu->tm_hour,stu->tm_min,stu->tm_sec);
			//printf("%s\n",time);
			close(fd);
			
			
			sprintf(buf,"STOR %s\n",temp);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			send(datafd,file,strlen(file),0);
			close(datafd);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			
			/*sprintf(buf,"PWD\n");
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			char path[4096] = {};
			sscanf(buf,"257 %s",path);
			printf("--%s\n",path);
			printf("--%s\n",time);
			printf("--%s\n",temp);*/
			
			sprintf(buf,"MDTM %s ./%s\n",time,temp);
			send(sockfd,buf,strlen(buf),0);
			bzero(buf,sizeof(buf));
			recv(sockfd,buf,sizeof(buf),0);
			printf("service:%s",buf);
			
		}
	}
}
